<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class info_art extends Admin_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('info_cate_m');
        $this->load->model('info_art_m');
        /** 检查登陆 */
        if (!$this->auth->is_admin()) {
            show_message('非管理员或未登录', site_url('admin/login/do_login'));
        }
    }

    /**
     * 文章搜索
     */
    public function search() {
        //查找用户
        $data['title'] = '用户搜索';
        $data['act'] = $this->uri->segment(3);
        if ($_POST) {
            $data['info_list'] = $this->info_art_m->search_info_by_title($this->input->post('username'));
        }
        $this->load->view('info_cate', $data);
    }

    /**
     * 管理文章分类
     * @param type $page
     */
    public function index($page = 1) {
        $data['title'] = '信息管理';
        //分页
        $limit = 20;
        $config['uri_segment'] = 4;
        $config['use_page_numbers'] = TRUE;
        $config['base_url'] = site_url('admin/info_art/index/');
        $config['total_rows'] = $this->db->count_all('info_art');
        $config['per_page'] = $limit;
        $config['prev_link'] = '&larr;';
        $config['prev_tag_open'] = '<li class=\'prev\'>';
        $config['prev_tag_close'] = '</li';
        $config['cur_tag_open'] = '<li class=\'active\'><span>';
        $config['cur_tag_close'] = '</span></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['next_link'] = '&rarr;';
        $config['next_tag_open'] = '<li class=\'next\'>';
        $config['next_tag_close'] = '</li>';
        $config['first_link'] = '首页';
        $config['first_tag_open'] = '<li class=\'first\'>';
        $config['first_tag_close'] = '</li>';
        $config['last_link'] = '尾页';
        $config['last_tag_open'] = '<li class=\'last\'>';
        $config['last_tag_close'] = '</li>';
        $config['num_links'] = 10;

        $this->load->library('pagination');
        $this->pagination->initialize($config);

        $start = ($page - 1) * $limit;
        $data['pagination'] = $this->pagination->create_links();

        $data['info_list'] = $this->info_art_m->get_all_info($start, $limit);

        $data['csrf_name'] = $this->security->get_csrf_token_name();
        $data['csrf_token'] = $this->security->get_csrf_hash();
//        var_dump($data);die;
        $data['act'] = 'index';
        $this->load->view('info_cate', $data);
    }

    public function del($topic_id, $node_id, $uid) {
        $data['title'] = '删除贴子';
        //$this->myclass->notice('alert("确定要删除此话题吗！");');
        //删除贴子及它的回复
        if ($this->topic_m->del_topic($topic_id, $node_id, $uid)) {
            $this->comment_m->del_comments_by_topic_id($topic_id, $uid);
            show_message('删除贴子成功！', site_url('admin/topics'), 1);
        }
    }

    /**
     * xiugai huoxinz 
     * @param type $id
     */
    public function edit($id) {
        $data['title'] = '修改信息';
        if ($_POST) {
            $str = array(
                'title' => $this->input->post('title'),
                'cateid' => $this->input->post('cateid'),
                'ishome' => $this->input->post('ishome'),
                'author' => $this->input->post('author'),
                'content' => $this->input->post('content'),
                'viewtimes' => $this->input->post('viewtimes'),
                'status' => $this->input->post('status'),
                'keywords' => $this->input->post('keywords')
            );
            if ($id) {
                $str['updatetime'] = time();
                if ($this->info_art_m->update_info($id, $str)) {
                    show_message('修改信息成功！', site_url('admin/info_art'), 1);
                }
            } else {
                $str['addtime'] = time();
                if ($this->info_art_m->add($str)) {
                    show_message('添加信息成功！', site_url('admin/info_art'), 1);
                }
            }
        }

        $data['cates'] = $this->info_cate_m->get_info_cate_list();
        $data['info'] = $this->info_art_m->get_detail_by_id_admin($id);
        $data['csrf_name'] = $this->security->get_csrf_token_name();
        $data['csrf_token'] = $this->security->get_csrf_hash();
        $this->load->view('info_cate_edit', $data);
    }

    public function set_top($topic_id, $is_top) {
        if ($this->topic_m->set_top($topic_id, $is_top)) {
            redirect('admin/topics/');
        }
    }

    public function batch_process() {
        $topic_ids = array_slice($this->input->post(), 0, -1);
        if ($this->input->post('batch_del')) {
            if ($this->db->where_in('topic_id', $topic_ids)->delete('topics')) {
                show_message('批量删除贴子成功！', site_url('admin/topics'), 1);
            }
        }
        if ($this->input->post('batch_approve')) {
            if ($this->db->where_in('topic_id', $topic_ids)->update('topics', array('is_hidden' => 0))) {
                show_message('批量审核贴子成功！', site_url('admin/topics'), 1);
            }
        }
    }

    public function approve($topic_id) {
        if ($this->db->where('topic_id', $topic_id)->update('topics', array('is_hidden' => 0))) {
            show_message('审核贴子成功！', site_url('admin/topics'), 1);
        } else {
            return false;
        }
    }

    /**
     * 分类相关
     * @param type $act
     * @param type $gid
     */
    public function cate($act, $gid = '') {
        $data['title'] = '信息分类管理';
        $data['act'] = $this->uri->segment(3) . $this->uri->segment(4);
        $this->load->model('info_cate_m');

        $orderBy = array();
        $data['cate_list'] = $this->info_cate_m->getListByCondition("info_cate", array(), $orderBy);
        $data['cate_info'] = $this->info_cate_m->get_cate_by_id($gid);
        $check_group = $this->info_cate_m->check_cate(@$_POST['cate_name']);
//        var_dump($data);
//        die;
        if ($act == 'add') {
            $data['title'] = '添加信息分类';
        }
        if ($act == 'edit') {
            $data['title'] = '编辑信息分类';
            if (@$_POST['commit_edit']) {
                $id = $this->input->post('id', true);
                $str = array(
                    'cate_name' => $this->input->post('cate_name', true),
                    'status' => $this->input->post('status', true),
                    'o' => $this->input->post('o', true),
                );
                if (!$id) {
                    $str['addtime'] = time();
                    if ($this->info_cate_m->add($str)) {
                        show_message('新增分类成功', site_url('admin/info_art/cate/index'), 1);
                    }
                } else {
                    if ($this->db->where('id', $id)->update('info_cate', $str)) {
                        show_message('修改分类成功', site_url('admin/info_art/cate/index'), 1);
                    }
                }
            }
        }
//        if ($act == 'del') {
//            if (@$data['group_info']['group_type'] > 1) {
//                if ($this->db->where('gid', $gid)->delete('user_groups')) {
//                    show_message('删除用户组成功', site_url('admin/users/group/index'), 1);
//                }
//            } else {
//                show_message('无法删除系统用户组', site_url('admin/users/group/index'));
//            }
//        }
        $data['csrf_name'] = $this->security->get_csrf_token_name();
        $data['csrf_token'] = $this->security->get_csrf_hash();
        $this->load->view('info_cate', $data);
    }

}
